#include "hal_floor.h"
#include "hk32c030xx_gpio.h"

static void floor_init() {
	
	GPIO_InitTypeDef floor_key;
	RCC_AHBPeriphClockCmd(FLOOR_RCU, ENABLE);
	
	floor_key.GPIO_Pin = JT_KEY_PIN | FLOOR_DOOR_CHECK_PIN | FLOOR_UP_LINMIT_PIN | 
#if MASTER_FOR_TEST
					FLOOR_DOWN_LINMIT_PIN;
#else
					FLOOR_DOWN_LINMIT_PIN | FLOOR_REACH_PIN;

#endif
	floor_key.GPIO_Mode = GPIO_Mode_IN;
	floor_key.GPIO_OType = GPIO_OType_PP;
	floor_key.GPIO_Speed = GPIO_Speed_10MHz;
	floor_key.GPIO_PuPd = GPIO_PuPd_NOPULL;
	floor_key.GPIO_Schmit = GPIO_Schmit_Disable;
	GPIO_Init(FLOOR_KEY_BASE, &floor_key);
	
#if MASTER_FOR_TEST
	RCC_AHBPeriphClockCmd(FLOOR_REACH_RCU, ENABLE);
	floor_key.GPIO_Pin = FLOOR_REACH_PIN;
	
	GPIO_Init(FLOOR_REACH_BASE, &floor_key);
#endif
}

static uint8_t floor_jt_scan() {
	if(GPIO_ReadInputDataBit(FLOOR_KEY_BASE,JT_KEY_PIN))
		return 0;
	else 
		return 1;
}

static uint8_t floor_door_check() {

	if(GPIO_ReadInputDataBit(FLOOR_KEY_BASE,FLOOR_DOOR_CHECK_PIN))
		return 0;
	else 
		return 1;
}

static uint8_t floor_limit_check() {

	if(GPIO_ReadInputDataBit(FLOOR_KEY_BASE,FLOOR_UP_LINMIT_PIN) == 0) {
		return FLOOR_STATE_UP_LIMIT;
	}
		
	if(GPIO_ReadInputDataBit(FLOOR_KEY_BASE,FLOOR_DOWN_LINMIT_PIN) == 0) {
		return FLOOR_STATE_DOWN_LIMIT;
	}
	
	return FLOOR_STATE_NORMAL;
}

static uint8_t floor_check_reach() {

	if(GPIO_ReadInputDataBit(FLOOR_REACH_BASE,FLOOR_REACH_PIN))
		return 0;
	else 
		return 1;
}

const floor_struct_t g_floor_node_ = {
	floor_init,
	floor_door_check,
	floor_limit_check,
	floor_check_reach,
	floor_jt_scan
};
